package com.example.clientsideapplication;

import java.sql.Date;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;

public class ViewMerchantOfferActivity extends Activity {
	private String serverAddress;
	private int serverSocket;
	private ArrayList<Offer> offerArrayList;
	private ListView offerViewList;
	private int id;
	private String productName;
	private double productPrice;
	private String productInfo;
	private Date startDate, endDate;
	private int startDay, startMonth, startYear;
	private int endDay, endMonth, endYear;
	private String category;
	private Context context;
	private AlertDialog.Builder builder;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.view_offer);

		context = getApplicationContext();
		
		Intent intent = getIntent();
		serverAddress = intent.getStringExtra("serverAddress");
		serverSocket = intent.getIntExtra("serverSocket", 21111);
		
		offerArrayList = (ArrayList<Offer>) intent.getSerializableExtra("offerArrayList");
			
		offerViewList = (ListView) findViewById(R.id.offer_list);
		offerViewList.setAdapter(new OfferListAdapter(this, R.layout.row_list_layout, offerArrayList));
		registerForContextMenu(offerViewList);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.view_offer_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

		switch (item.getItemId()) {
		
		case R.id.modifyOfferMenu:
			Intent intent = new Intent(context, ModifyOfferActivity.class);
			intent.putExtra("serverAddress", serverAddress);
			intent.putExtra("serverSocket", serverSocket);
			
			int i = info.position;
			id = offerArrayList.get(i).getIdOffer();
			productName = offerArrayList.get(i).getProductName();
			productPrice = offerArrayList.get(i).getProductPrice();
			productInfo = offerArrayList.get(i).getProductInfo();
			startDate = offerArrayList.get(i).getStartDate();
			endDate = offerArrayList.get(i).getEndDate();
			category = offerArrayList.get(i).getProductCategory();
			
			startDay = startDate.getDate();
			startMonth = startDate.getMonth();
			startYear = startDate.getYear();
			endDay = endDate.getDate();
			endMonth = endDate.getMonth();
			endYear = endDate.getYear();
			
			intent.putExtra("productName", productName);
			intent.putExtra("productPrice", productPrice);
			intent.putExtra("productInfo", productInfo);
			intent.putExtra("category", category);
			intent.putExtra("giornoInizio", startDay);
			intent.putExtra("meseInizio", startMonth);
			intent.putExtra("annoInizio", startYear);
			intent.putExtra("giornoFine", endDay);
			intent.putExtra("meseFine", endMonth);
			intent.putExtra("annoFine", endYear);
			intent.putExtra("idOffer", id);

			startActivity(intent);

			finish();

			return true;
		
		case R.id.deleteOfferMenu:
			int indexDelete = info.position;
			int idOfferDelete = offerArrayList.get(indexDelete).getIdOffer();

			builder = new AlertDialog.Builder(this);
			builder.setMessage("Sei sicuro di voler cancellare questa offerta?");
			builder.setCancelable(false);
			builder.setPositiveButton("Yes", new MyClickListener(idOfferDelete, indexDelete) {
						public void onClick(DialogInterface dialog, int id) {
							Intent intent = new Intent(context, DeleteOfferService.class);
							intent.putExtra("serverAddress", serverAddress);
							intent.putExtra("serverSocket", serverSocket);
							intent.putExtra("idOffer", getId());
							intent.putExtra("index", getIndex());
							
							startService(intent);

							offerArrayList.remove(getIndex());
							offerViewList = (ListView) findViewById(R.id.offer_list);
							offerViewList.setAdapter(new OfferListAdapter(
									ViewMerchantOfferActivity.this,
									R.layout.row_list_layout, offerArrayList));
							registerForContextMenu(offerViewList);
							
							int duration = Toast.LENGTH_LONG;
							Toast toast = Toast
									.makeText(
											context,
											"Cancellazione effettuata con successo.",
											duration);
							toast.show();
						}
					});
			builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
							int duration = Toast.LENGTH_LONG;
							Toast toast = Toast
									.makeText(
											context,
											"Cancellazione annullata.",
											duration);
							toast.show();
						}
					});

			AlertDialog alert = builder.create();
			alert.show();
			return true;
			
		case R.id.viewInfoMenu:
			int indexModify = info.position;			
			Intent offerIntent= new Intent(ViewMerchantOfferActivity.this, ViewMerchantOfferInfo.class);
			Offer modifyOffer = offerArrayList.get(indexModify);
		    offerIntent.putExtra("offer", modifyOffer);
			startActivity(offerIntent);
			return true;
			
		default:
			return super.onContextItemSelected(item);
		}
	}
}

class MyClickListener implements OnClickListener {

	private int id;
	private int index;

	public MyClickListener(int id, int index) {
		this.id = id;
		this.index = index;
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		
	}

	public int getId() {
		return id;
	}

	public int getIndex() {
		return index;
	}

}